<template>
  <div class="login">
    <div
      style="
        margin: 180px auto;
        background-color: #fff;
        width: 350px;
        height: 240px;
        padding: 20px;
        border-radius: 10px;
      "
    >
      <div class="title"> <i class="el-icon-s-promotion"></i> 注册</div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username">
          <el-input
            size="medium"
            style="margin: 10px auto"
            prefix-icon="el-icon-user"
            v-model="user.username"
          ></el-input
        ></el-form-item>
        <el-form-item prop="password"
          ><el-input
            size="medium"
            style="margin: 10px auto"
            prefix-icon="el-icon-lock"
            show-password
            v-model="user.password"
          ></el-input
        ></el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button
            type="primary"
            size="small"
            autocomplete="off"
            @click="login"
            >注册</el-button
          >
          <el-button type="warning" size="small" autocomplete="off" @click="toLogin"
            >返回登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
export default {
  name: "Register",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    toLogin(){
      this.$router.push('/login')
    },
    login() {
      this.$refs['userForm'].validate((valid) => {
        //如果合法
        if (valid) {
          request.put("/user/register", this.user).then((res) => {
            if (res.status===200) {
                this.$message.success("注册成功");
              this.$router.push("/login");
            } else {
               this.$message.error(res.msg);
            }
          });
        }
        //非法
        else {
            return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.login {
  height: 100vh;
  overflow: hidden;
  background-image: linear-gradient(to bottom right, #d32144, #151fb4);
}
.title {
  text-align: center;
  font-size: 24px;
  margin: 20px auto;
}
</style>>